#include "SQLDropSequenceStatement.h"
#include "SQLDDLStatement.h"
#include "../../visitor/SQLASTVisitor.h"
#include "../SQLName.h"
#include "../../../DbType.h"
#include "../../../utils/instanceof.h"
#include "../expr/SQLPropertyExpr.h"

SQLDropSequenceStatement::SQLDropSequenceStatement()
{
}

SQLDropSequenceStatement::SQLDropSequenceStatement(DbType_ptr  dbType)
    : SQLStatementImpl(dbType)
{
}

void SQLDropSequenceStatement::accept0(SQLASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(SQLDropSequenceStatement)))
  {
    acceptChild(visitor, name);
  }
  visitor->endVisit(SharedObject(SQLDropSequenceStatement));
}

SQLObject_list_ptr  SQLDropSequenceStatement::getChildren()
{
  SQLObject_list_ptr  children = std::make_shared<std::list<SQLObject_ptr>>();
  if (name != NULL)
  {
    children->push_back(std::dynamic_pointer_cast<SQLObject>(name));
  }
  return children;
}

SQLName_ptr SQLDropSequenceStatement::getName()
{
  return name;
}

void SQLDropSequenceStatement::setName(SQLName_ptr name)
{
  this->name = name;
}

BOOL_ptr SQLDropSequenceStatement::isIfExists()
{
  return ifExists;
}

void SQLDropSequenceStatement::setIfExists(BOOL_ptr ifExists)
{
  this->ifExists = ifExists;
}

string_ptr  SQLDropSequenceStatement::getSchema()
{
  SQLName_ptr name = getName();
  if (name == NULL)
  {
    return NULL;
  }

  if (instanceof <SQLName, SQLPropertyExpr>(name))
  {
    return (std::dynamic_pointer_cast<SQLPropertyExpr>(name))->getOwnernName();
  }

  return NULL;
}

BOOL_ptr SQLDropSequenceStatement::replace(SQLExpr_ptr expr, SQLExpr_ptr target)
{
  if (name == expr)
  {
    setName(std::dynamic_pointer_cast<SQLName>(target));
    return BOOL::TRUE;
  }

  return BOOL::FALSE;
}

SQLDDLStatement::DDLObjectType SQLDropSequenceStatement::getDDLObjectType()
{
  return SQLDDLStatement::DDLObjectType::SEQUENCE;
}
